const router = require('koa-router')();
const koaBody = require('koa-body')();
const request = require('superagent'); //不要忘记npm install
var config=require('../config/app.config');

module.exports = router.post('/api/cms/topics/qu33ery', koaBody,(ctx) => {
    console.log(ctx.request.body);
    // => POST body
    ctx.body = JSON.stringify(ctx.request.body);
    console.log(ctx.body)
    // 调用远程接口获取用户授权信息
    request
    .post(config.host+'/api/cms/topics/query')
    .send({ code: 'Manny', merchantNo: 'wwew' })
    .set('X-API-Key', 'foobar')
    .set('Accept', 'application/json')
    .set('Content-Type', 'application/json')
    .end(function(err, res) {
        var json = eval("("+res.text+")");
        if (json.ok) {
            console.log('yay got ' + JSON.stringify(res.body));
            ctx.render('index', { title: 'Nunjucks', content: 'Feifeiyu yeah!' });
        } else {
            console.log('Oh no! error ' + err);
        }
    });
  }).post('/api/cms/viewpoints/add', koaBody,(ctx) =>{
    ctx.body = JSON.stringify(ctx.request.body);
    console.log(ctx.body)
    // 添加圈子
    request
        .post(config.host+'/api/cms/viewpoints/add')
        .send(ctx.body)
        .set('X-API-Key', 'foobar')
        .set('Accept', 'application/json')
        .set('Content-Type', 'application/json')
        .end(function(err, res) {
            console.log(err);
             console.log('/api/cms/viewpoints/add:' + JSON.stringify(res));
             console.log('/api/cms/viewpoints/addrrrr:' + err);
        });

})
